package com.cj.http;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cj.Mapper.BooksMapper;
import com.cj.models.BookModel;
import com.cj.models.ProductModel;
import com.cj.models.ResultModel;
import com.cj.services.ProdService;
import org.apache.tomcat.util.bcel.classfile.ClassFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
//import sun.tools.java.ClassNotFound;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/prods")
public class ProdsForGet {

    @Autowired
    public ProdsForGet(ProdService prodService){
        this.prodService = prodService;
    }

//    @Autowired @Qualifier("v2")
    @Resource(type =Map.class) @Qualifier("v1")
    private Map<String,String> cc;
    @GetMapping("/test")
    public String test(HttpServletRequest request) throws ClassNotFoundException {
        String t= request.getParameter("t");
        if(t.equals("1")){
            throw new ClassNotFoundException("excep 1");
        }else{
            throw new ClassFormatException("excep 2");
        }

    }
    private final ProdService prodService;
    @Autowired
    private BooksMapper booksMapper;
    @GetMapping("")
    public ResultModel prodList(@RequestParam(value = "c",
            required = false,defaultValue = "0") int c,
                                @RequestParam(value = "p",required = false,defaultValue = "0") int p){
        QueryWrapper<BookModel> bookModelQueryWrapper = new QueryWrapper<BookModel>();
        if (c > 0) {
            bookModelQueryWrapper.eq("book_kind",c);
        }
//        bookModelQueryWrapper.eq("b.book_kind=k.kind_id","");
        bookModelQueryWrapper.orderByDesc("book_id");

        Page<BookModel> page = new Page<>(p,5);
        System.out.println(bookModelQueryWrapper.getSqlSegment());


        List<BookModel> bookModelList = booksMapper.selectBookByCid(page,bookModelQueryWrapper);


        return new ResultModel("success",bookModelList,page);

//        IPage getPage = booksMapper.selectPage(page, bookModelQueryWrapper);
//        System.out.println("总页数:"+getPage.getPages());
//        System.out.println("总条数:"+getPage.getTotal());
//        System.out.println("当前页:"+getPage.getCurrent());

//        return new ResultModel("success",getPage.getRecords());
    }
    @GetMapping("{id:\\d+}")
    public ResultModel getProd(@PathVariable int id) throws InterruptedException {
        BookModel bookModel = booksMapper.selectById(id);
        if(bookModel == null){
            return new ResultModel("error","no book");
        }else {
            return new ResultModel("success",bookModel);
        }
    }
}
